package easy;

import java.util.ArrayList;
import java.util.List;

public class Solution_1417 {

    public String reformat(String s) {
        char[] chars = s.toCharArray();
        List<Character> set_1 = new ArrayList<>();
        List<Character> set_2 = new ArrayList<>();
        for (char c : chars) {
            if (97 <= c && c <= 122) {
                set_1.add(c);
            } else {
                set_2.add(c);
            }
        }
        if (Math.abs(set_1.size() - set_2.size()) > 1) {
            return "";
        }
        char[] array = new char[s.length() + 1];
        for (int i = 0, index = 1; i < Math.min(set_1.size(), set_2.size()); i++) {
            array[index++] = set_1.get(i);
            array[index++] = set_2.get(i);
        }
        if (set_1.size() > set_2.size()) {
            array[array.length - 1] = set_1.get(set_1.size() - 1);
        } else if (set_1.size() < set_2.size()) {
            array[0] = set_2.get(set_2.size() - 1);
            return new String(array, 0, s.length());
        }
        return new String(array, 1, s.length());
    }

    public static void main(String[] args) {
        Solution_1417 model = new Solution_1417();
        System.out.println(model.reformat("a0b1c2"));
        System.out.println(model.reformat("ab123"));
        System.out.println(model.reformat("77"));
        System.out.println(model.reformat("leetcode"));
        System.out.println(model.reformat("1229857369"));
        System.out.println(model.reformat("covid2019"));
    }
}
